package com.RedRiver.hospitalsystem.personal_service.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.AccountException;
import com.RedRiver.hospitalsystem.personal_service.common.result.PageResult;
import com.RedRiver.hospitalsystem.personal_service.common.util.Md5Util;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Account;
import com.RedRiver.hospitalsystem.personal_service.service.AccountService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.AccountForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.Login;
import com.RedRiver.hospitalsystem.personal_service.service.Form.PageForm;
import com.RedRiver.hospitalsystem.personal_service.service.info.AccountInfo;
import com.RedRiver.hospitalsystem.personal_service.service.info.AccountPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/account")
@Api(tags = "工作人员后台用户接口")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @ApiOperation("医生登录用户")
    @PostMapping("/login")
    public Result<String> login(Login loginAccount){
       return accountService.login(loginAccount);
    }

    @ApiOperation("根据id查看用户信息")
    @GetMapping("/findAccount/{accountId}")
    public Result findAccount(@ApiParam("请输入需要查询的Id") @PathVariable("accountId") Integer accountId){
        Account account = accountService.getOne(new QueryWrapper<Account>().eq("account_id", accountId));
        if (account == null) throw new AccountException(Personal_serviceResultCode.ACCOUNT_USER_NOEXISTED);
        return Result.getInstance(AccountInfo.class).setCode(200).setMsg("查询成功").setData(BeanUtil.copyProperties(account, AccountInfo.class));
    }

    @ApiOperation("查看所有用户信息分页查询")
    @PostMapping("/findAllAccount")
    public Result<IPage> findAllAccount(@RequestBody PageForm form){
        IPage<AccountPageInfo> byPage = accountService.findPageAllAccount(form);
        return  Result.getInstance(IPage.class).setMsg("分页成功").setData(byPage);
    }

    @ApiOperation("新增一个用户")
    @PostMapping("/createAccount")
    public Result createAccount(@RequestBody AccountForm accountForm){
        return accountService.createAccount(accountForm);
    }

    @ApiOperation("根据用户名更改密码")
    @PostMapping("/updateAccount")
    public Result updateAccount(@RequestBody AccountForm accountForm){
        return accountService.update(accountForm);
    }

    @ApiOperation("删除用户")
    @GetMapping("/delAccount/{accountId}")
    public Result delAccount(@ApiParam("请输入需要删除的用户的Id") @PathVariable("accountId") Integer accountId){
        return accountService.delete(accountId);
    }

}

